package com.yonyougov.devops.cruxcisupport.role;

import com.yonyougov.devops.cruxcisupport.component.UserStore;
import com.yonyougov.devops.cruxcisupport.role.vo.LinkedRoleRespVo;
import com.yonyougov.devops.cruxcisupport.role.vo.NoLinkRoleVo;
import com.yonyougov.devops.cruxcisupport.role.vo.NotLinkedRoleByUserIdDto;
import com.yonyougov.devops.fwc.core.dao.BaseDao;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

/**
 * t_role服务实现
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<RoleEntity, BigInteger> implements RoleService {
    private final RoleDao roleDao;

    private final UserStore userStore;

    public RoleServiceImpl(RoleDao roleDao, UserStore userStore) {
        this.roleDao = roleDao;
        this.userStore = userStore;
    }

    @Override
    public BaseDao<RoleEntity, BigInteger> getDao() {
        return roleDao;
    }

    //<!--用户自定义代码开始-->
    @Override
    public List<RoleEntity> getCurrentUserRoles() throws Exception {
        BigInteger userId = userStore.getCurrentUser().getId();
        if (null == userId) {
            throw new Exception("获取当前用户id失败");
        }
        return roleDao.getCurrentUserRoles(userId);
    }

    @Override
    public List<BigInteger> getCurrentUserRoleIds() throws Exception {
        BigInteger userId = userStore.getCurrentUser().getId();
        if (null == userId) {
            throw new Exception("获取当前用户id失败");
        }
        return roleDao.getCurrentUserRoleIds(userId);
    }

    @Override
    public Pagination<NoLinkRoleVo> findNoLinkRoleByUserId(NotLinkedRoleByUserIdDto notLinkedRoleByUserIdDto) {
        List<NoLinkRoleVo> datas = roleDao.findNoLinkRoleByUserId(notLinkedRoleByUserIdDto);
        int totalCount = roleDao.findNoLinkRoleByUserIdCount(notLinkedRoleByUserIdDto);
        return new Pagination<>(notLinkedRoleByUserIdDto.getMax(), notLinkedRoleByUserIdDto.getPageNum(), totalCount, datas);
    }

    @Override
    public List<LinkedRoleRespVo> findLinkedRoleByUserId(BigInteger userId) {
        return roleDao.findLinkedRoleByUserId(userId);
    }

    //<!--用户自定义代码结束-->
}
